﻿using GKQC.Common;
using GKQC.DbHelper.DbModels;
using GKQC.Language;
using NPOI.SS.UserModel;
using NPOI.Util;
using Org.BouncyCastle.Asn1.Cmp;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GKQC.Ort.Reports
{
    public class BaseReport
    {
        public static string ErrorMsg { get; set; } = "";
        public IWorkbook GenReport(OrtHeadModel head, ReportSetModel reportSet, string path, bool isCreateFile)
        {
            string errorMsg = string.Empty;
            string fileName = Settings.GetReportFileName(path, reportSet.Path, out errorMsg);
            if (!string.IsNullOrWhiteSpace(errorMsg))
            {
                QMessageBox.Show(errorMsg, "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return null;
            }

            string sysTempFileName = sysTempFileName =  Settings.GetTempPath()
                + "\\"
                + (isCreateFile ? OrtReportFactory.GetExcelTempFileName(head, reportSet) : Guid.NewGuid().ToString())
                + "-"
                + fileName.Substring(fileName.LastIndexOf('\\') + 1)
                ;

            try
            {
                File.Copy(fileName, sysTempFileName, true);
            }
            catch(Exception ex)
            {
                if (!QcFormBase.Version.StartsWith("2"))
                {
                    sysTempFileName = sysTempFileName = Settings.GetTempPath()
                        + "\\"
                        + OrtReportFactory.GetExcelTempFileName(head, reportSet) + Path.GetRandomFileName()
                        + "-"
                        + fileName.Substring(fileName.LastIndexOf('\\') + 1)
                        ;
                    File.Copy(fileName, sysTempFileName, true);
                }
                else
                    throw ex;
            }

            IWorkbook workbook = null;
            using (FileStream fs = new FileStream(sysTempFileName, FileMode.Open, FileAccess.Read))  //路径，打开权限，读取权限
            {
                workbook = WorkbookFactory.Create(new FileInputStream(fs));
                fs.Close();
                if (!isCreateFile) File.Delete(sysTempFileName);
            }
            ISheet sheet = workbook.GetSheetAt(0);

            try { GenReportSheet(workbook, sheet, head); } catch (Exception ex) { Logger.LogError(ex); }

            if (isCreateFile)
            {
                using (FileStream fs = new FileStream(sysTempFileName, FileMode.Create, FileAccess.Write))
                {
                    MemoryStream stream = new MemoryStream();
                    workbook.Write(stream);
                    var buf = stream.ToArray();
                    fs.Write(buf, 0, buf.Length);
                    fs.Flush();
                }

                Process.Start(sysTempFileName);
            }

            return workbook;
        }

        public virtual void GenReportSheet(IWorkbook workbook, ISheet sheet, OrtHeadModel head)
        {
        }
    }
}
